<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
<title>SympyCore Release 0.2 Demo</title>
<meta name="authors" content="Pearu Peterson" />
<style type="text/css">

/*
:Author: Pearu Peterson
:Contact: pearu.peterson AT gmail DOT com
:Copyright: This stylesheet has been placed in the public domain.

Stylesheet for use with Docutils.
*/

@import url(html4css1.css);

/* Your customizations go here.  For example: */

body {
    margin:0px;
    padding:25px;
    background-color: #ccc;
    font-size: 13px; font-family: arial, sans-serif;
    line-height:1.5em;
}

div.document {
    max-width: 700px;
    color: #000;
    background-color: #fff;
    padding:25px;
    border:5px solid #ddd;
}

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

pre, tt {
  font-family: consolas, lucida console, courier new, monospace;
  color: #000080 ;
}

pre.literal-block, pre.doctest-block {
  line-height:1.3em;
  border-top:1px solid #ccc;
  border-bottom:1px solid #ccc;
  background-color:#f0f0f0;
  color: #000080 ;
}

div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
  margin: 2em ;
  border: medium outset ;
  padding: 0.5em;
  background-color: #fffacd;  
}

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

</style>
</head>
<body>
<div class="document" id="sympycore-release-0-2-demo">
<h1 class="title">SympyCore Release 0.2 Demo</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Authors:</th>
<td>Pearu Peterson</td></tr>
<tr class="field"><th class="docinfo-name">Created:</th><td class="field-body">February 2008</td>
</tr>
<tr class="field"><th class="docinfo-name">Website:</th><td class="field-body"><a class="reference" href="http://sympycore.googlecode.com/">http://sympycore.googlecode.com/</a></td>
</tr>
<tr><th class="docinfo-name">Version:</th>
<td>sympycore 0.2-svn.
Other versions: <a class="reference" href="http://sympycore.googlecode.com/svn-history/r818/trunk/doc/html/demo0_1.html">0.1</a>.</td></tr>
</tbody>
</table>
<!-- -*- rest -*- -->
<div class="sidebar">
<p class="first sidebar-title">Table of contents</p>
<div class="contents local last topic">
<ul class="simple">
<li><a class="reference" href="#getting-started" id="id3" name="id3">Getting started</a></li>
<li><a class="reference" href="#constructing-symbolic-expressions" id="id4" name="id4">Constructing symbolic expressions</a></li>
<li><a class="reference" href="#manipulations-with-symbolic-expressions" id="id5" name="id5">Manipulations with symbolic expressions</a></li>
<li><a class="reference" href="#transformation-methods" id="id6" name="id6">Transformation methods</a></li>
<li><a class="reference" href="#arithmetic-methods" id="id7" name="id7">Arithmetic methods</a></li>
<li><a class="reference" href="#calculus-methods" id="id8" name="id8">Calculus methods</a></li>
<li><a class="reference" href="#polynomial-rings" id="id9" name="id9">Polynomial rings</a></li>
<li><a class="reference" href="#matrix-rings" id="id10" name="id10">Matrix rings</a></li>
<li><a class="reference" href="#physical-units" id="id11" name="id11">Physical units</a></li>
</ul>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id3" id="getting-started" name="getting-started">Getting started</a></h1>
<p>This document gives a short overview of SympyCore basic features. For
more information see the <a class="reference" href="http://sympycore.googlecode.com/svn/trunk/doc/html/userguide.html">SympyCore User's Guide</a>.</p>
<p>To use the <tt class="docutils literal"><span class="pre">sympycore</span></tt> package from Python, one must import it:</p>
<pre class="doctest-block">
&gt;&gt;&gt; from sympycore import *
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id4" id="constructing-symbolic-expressions" name="constructing-symbolic-expressions">Constructing symbolic expressions</a></h1>
<p>Generally speaking, symbolic expressions consist of symbols and
operation between them. To create symbolic expressions using
SympyCore, one can either create symbol objects and perform operations
between them:</p>
<pre class="doctest-block">
&gt;&gt;&gt; x = Symbol('x')
&gt;&gt;&gt; y = Symbol('y')
&gt;&gt;&gt; z = Symbol('z')
&gt;&gt;&gt; x + y
Calculus('x + y')
</pre>
<p>or one can use symbolic expression parser to construct symbolic
expressions from a string:</p>
<pre class="doctest-block">
&gt;&gt;&gt; Calculus('x + y')
Calculus('x + y')
</pre>
<p>SympyCore converts symbolic expressions to a canonical form that is
efficient for further manipulations and are also often obvious
simplifications that users may expect:</p>
<pre class="doctest-block">
&gt;&gt;&gt; x + x
Calculus('2*x')
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; x - x
Calculus('0')
</pre>
<p>General symbolic arithmetic expressions are instances of the Calculus
class. Using the <tt class="docutils literal"><span class="pre">print</span></tt> statement (or <tt class="docutils literal"><span class="pre">str()</span></tt>) hides this information:</p>
<pre class="doctest-block">
&gt;&gt;&gt; print x + y
x + y
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id5" id="manipulations-with-symbolic-expressions" name="manipulations-with-symbolic-expressions">Manipulations with symbolic expressions</a></h1>
<p>The most obvious manipulation task applied to symbolic expressions, is
substitution -- replacing a sub-expression of a given expression with a
new expression. For example,</p>
<pre class="doctest-block">
&gt;&gt;&gt; expr = x + y
&gt;&gt;&gt; print expr.subs(y, sin(x))
x + sin(x)
</pre>
<p>Other tasks include accessing parts of symbolic expressions:</p>
<pre class="doctest-block">
&gt;&gt;&gt; sorted(expr.args)
[Calculus('x'), Calculus('y')]
</pre>
<p>and constructing new expressions:</p>
<pre class="doctest-block">
&gt;&gt;&gt; print Mul(*expr.args)
x*y
</pre>
<p>An important presumption for implementing various algorithms is pattern
matching. Pattern matching means that given a pattern expression, the
pattern match method should first decide whether an expression can be
expressed in a form that the pattern defines, and second. it should
return information what sub-expression parts correspond to the pattern
sub-expressions. For example, given a pattern</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = Symbol('w')
&gt;&gt;&gt; pattern = x * w ** 3
</pre>
<p>where symbol <tt class="docutils literal"><span class="pre">w</span></tt> is assumed to match any sub-expression, then expressions</p>
<pre class="doctest-block">
&gt;&gt;&gt; expr1 = x*sin(y)**3
&gt;&gt;&gt; expr2 = x*(x+y)**3
</pre>
<p>do match the given pattern:</p>
<pre class="doctest-block">
&gt;&gt;&gt; d1 = expr1.match(pattern, w)
&gt;&gt;&gt; print d1
{Calculus('w'): Calculus('sin(y)')}
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; d2 = expr2.match(pattern, w)
&gt;&gt;&gt; print d2
{Calculus('w'): Calculus('x + y')}
</pre>
<p>The result of <tt class="docutils literal"><span class="pre">match</span></tt> method, when the match is found, is a dictionary
with the property</p>
<pre class="doctest-block">
&gt;&gt;&gt; pattern.subs(d1.items())==expr1
True
&gt;&gt;&gt; pattern.subs(d2.items())==expr2
True
</pre>
<p>If no match is found, then the <tt class="docutils literal"><span class="pre">match</span></tt> returns <tt class="docutils literal"><span class="pre">None</span></tt>:</p>
<pre class="doctest-block">
&gt;&gt;&gt; print (y*x**2).match(pattern, w)
None
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id6" id="transformation-methods" name="transformation-methods">Transformation methods</a></h1>
<p>The most common transformation task is expansion of sub-expressions by
opening parenthesis:</p>
<pre class="doctest-block">
&gt;&gt;&gt; expr = (x+y)*z
&gt;&gt;&gt; print expr
z*(x + y)
&gt;&gt;&gt; print expr.expand()
x*z + y*z
</pre>
<p>In general, the <tt class="docutils literal"><span class="pre">expand</span></tt> method expands products of sums and
integer powers of sums:</p>
<pre class="doctest-block">
&gt;&gt;&gt; expr = (x+y)*(1+x)**3
&gt;&gt;&gt; print expr.expand()
x + y + x**4 + 3*x**2 + 3*x**3 + 3*x*y + 3*y*x**2 + y*x**3
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id7" id="arithmetic-methods" name="arithmetic-methods">Arithmetic methods</a></h1>
<p>SympyCore provides exact rational and complex numbers:</p>
<pre class="doctest-block">
&gt;&gt;&gt; Calculus('3/12')
Calculus('1/4')
&gt;&gt;&gt; print (2 + 3*I/4)**4
721/256 + 165/8*I
</pre>
<p>Fractional powers of integers are evaluated to simpler
expressions when possible:</p>
<pre class="doctest-block">
&gt;&gt;&gt; Calculus('8**(1/3)')
Calculus('2')
&gt;&gt;&gt; Calculus('243**(1/5)')
Calculus('3')
</pre>
<p>SympyCore supports converting symbolic expressions with exact numbers
such as integers and rational numbers to expressions with arbitrary
precision floating-point numbers:</p>
<pre class="doctest-block">
&gt;&gt;&gt; expr = 2*pi + E**x
&gt;&gt;&gt; print expr
E**x + 2*pi
&gt;&gt;&gt; print expr.evalf(5)
6.2832 + 2.7183**x
&gt;&gt;&gt; print expr.evalf(25)
6.283185307179586476925287 + 2.718281828459045235360287**x
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id8" id="calculus-methods" name="calculus-methods">Calculus methods</a></h1>
<p>SympyCore provides methods to differentiate symbolic expressions:</p>
<pre class="doctest-block">
&gt;&gt;&gt; expr = x+sin(x*y)*x
&gt;&gt;&gt; print expr.diff(x)
1 + sin(x*y) + x*y*cos(x*y)
</pre>
<p>as well as integrate symbolic expression representing polynomials:</p>
<pre class="doctest-block">
&gt;&gt;&gt; expr = x + 3*z*x**2
&gt;&gt;&gt; print expr.integrate(x)
1/2*x**2 + z*x**3
&gt;&gt;&gt; print expr.integrate((x, 2, y))
1/2*y**2 + z*(y**3 - 8) - 2
</pre>
<p>SympyCore implements the elementary functions <tt class="docutils literal"><span class="pre">exp</span></tt>, <tt class="docutils literal"><span class="pre">log</span></tt>,
<tt class="docutils literal"><span class="pre">sqrt</span></tt>, <tt class="docutils literal"><span class="pre">cos</span></tt>, <tt class="docutils literal"><span class="pre">sin</span></tt>, <tt class="docutils literal"><span class="pre">tan</span></tt>, <tt class="docutils literal"><span class="pre">cot</span></tt>, and simplifies
their values in basic cases:</p>
<pre class="doctest-block">
&gt;&gt;&gt; print log(10000,10)
4
&gt;&gt;&gt; print sin(5*pi/6)
1/2
&gt;&gt;&gt; print cos(x+pi/2)
-sin(x)
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id9" id="polynomial-rings" name="polynomial-rings">Polynomial rings</a></h1>
<p>SympyCore provides efficient ways to represent univariate and
multivariate polynomials. Currently there are two representation
supported. The first one is suitable for univariate dense polynomials:</p>
<pre class="doctest-block">
&gt;&gt;&gt; poly1 = UnivariatePolynomial([2,0,3,4])
&gt;&gt;&gt; poly2 = UnivariatePolynomial([0,1,0,5,6])
&gt;&gt;&gt; poly1
2 + 3*x**2 + 4*x**3
&gt;&gt;&gt; poly2
x + 5*x**3 + 6*x**4
&gt;&gt;&gt; poly1 + poly2
2 + x + 3*x**2 + 9*x**3 + 6*x**4
</pre>
<p>And the other representation is suitable for multivariate sparse
polynomials:</p>
<pre class="doctest-block">
&gt;&gt;&gt; P = PolynomialRing[(x,y)]
&gt;&gt;&gt; poly1 = P({(1,2):7, (300,4):5})
&gt;&gt;&gt; poly2 = P({(3,4):-7, (2,500):12})
&gt;&gt;&gt; poly1
PolynomialRing[(x, y), Calculus]('5*x**300*y**4 + 7*x*y**2')
&gt;&gt;&gt; print poly2
((-7))*x**3*y**4 + 12*x**2*y**500
&gt;&gt;&gt; print poly1 + poly2
5*x**300*y**4 + ((-7))*x**3*y**4 + 12*x**2*y**500 + 7*x*y**2
</pre>
<p>Here the <tt class="docutils literal"><span class="pre">PolynomialRing[symbols,</span> <span class="pre">Algebra]</span></tt> represents a factory of
a polynomial ring over <tt class="docutils literal"><span class="pre">Algebra</span></tt> with <tt class="docutils literal"><span class="pre">symbols</span></tt>.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id10" id="matrix-rings" name="matrix-rings">Matrix rings</a></h1>
<p>SympyCore supports representing rectangular matrix ring elements using
similar idea of ring factory:</p>
<pre class="doctest-block">
&gt;&gt;&gt; M = MatrixRing[(3,4)]
&gt;&gt;&gt; matrix = M({(1,2):x+y, (0,0):x+z})
&gt;&gt;&gt; print matrix
 x + z  0      0  0
     0  0  x + y  0
     0  0      0  0
</pre>
<p>Note that matrices are mutable in SympyCore and indexes start from 0:</p>
<pre class="doctest-block">
&gt;&gt;&gt; matrix[1,0] = 5
&gt;&gt;&gt; print matrix
 x + z  0      0  0
     5  0  x + y  0
     0  0      0  0
</pre>
<p>SympyCore provides <tt class="docutils literal"><span class="pre">SquareMatrix</span></tt> and <tt class="docutils literal"><span class="pre">PermutationMatrix</span></tt>
factories for convenience:</p>
<pre class="doctest-block">
&gt;&gt;&gt; SqM = SquareMatrix[3]
&gt;&gt;&gt; m = SqM({(0,0): 1, (2,1): 3, (2,2):6, (1,2):-2, (2,0): -1})
&gt;&gt;&gt; print m
  1  0   0
  0  0  -2
 -1  3   6
&gt;&gt;&gt; print PermutationMatrix[4]([2,1,3,0])
 0  0  1  0
 0  1  0  0
 0  0  0  1
 1  0  0  0
</pre>
<p>One can perform LU factorization on any rectangular matrix:</p>
<pre class="doctest-block">
&gt;&gt;&gt; p, l, u = m.lu()
&gt;&gt;&gt; print p
 1  0  0
 0  0  1
 0  1  0
&gt;&gt;&gt; print l
  1  0  0
 -1  1  0
  0  0  1
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; print u
 1  0   0
 0  3   6
 0  0  -2
</pre>
<p>The <tt class="docutils literal"><span class="pre">*</span></tt> denotes matrix multiplication:</p>
<pre class="doctest-block">
&gt;&gt;&gt; print p * l * u == m
True
</pre>
<p>SympyCore supports computing inverses of square
matrices:</p>
<pre class="doctest-block">
&gt;&gt;&gt; print m.inv()
   1     0    0
 1/3     1  1/3
   0  -1/2    0
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; m.inv() * m == SqM.one
True
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id11" id="physical-units" name="physical-units">Physical units</a></h1>
<p>SympyCore has a basic support for dealing with symbolic expressions with
units:</p>
<pre class="doctest-block">
&gt;&gt;&gt; mass1 = 5 * kilogram
&gt;&gt;&gt; mass2 = x * kilogram
&gt;&gt;&gt; print mass1 + mass2
(5 + x)*kg
</pre>
</div>
</div>
</body>
</html>
